#!/usr/bin/env python
# -*- coding: utf-8 -*-
# datetime: 31/3/2024 上午9:46
# Author: name
# File: custom_logger.py
# ide: PyCharm
# description: 日志工具类封装


import logging


class APILogger:
    def __init__(self, log_file='../../logs/api_test.log'):
        self.logger = logging.getLogger('API Test Logger')
        self.logger.setLevel(logging.DEBUG)

        # 创建文件处理器和控制台处理器
        file_handler = logging.FileHandler(log_file)
        console_handler = logging.StreamHandler()

        # 设置日志格式
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        file_handler.setFormatter(formatter)
        console_handler.setFormatter(formatter)

        # 添加处理器到日志对象
        self.logger.addHandler(file_handler)
        self.logger.addHandler(console_handler)

    def info(self, message):
        self.logger.info(message)

    def debug(self, message):
        self.logger.debug(message)

    def error(self, message):
        self.logger.error(message)

    def warning(self, message):
        self.logger.warning(message)

    def critical(self, message):
        self.logger.critical(message)


# 示例用法
if __name__ == "__main__":
    # 创建日志对象
    logger = APILogger()

    # 记录日志
    logger.info('This is an info message')
    logger.debug('This is a debug message')
    logger.error('This is an error message')
